package com.lpc.broadcastreceivertest;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;


/**
 * 静态广播带权限验证
 */
public class ThirdActivity extends AppCompatActivity {

    public static final String STATIC_ACTION = "com.lpc.broadcastreceivertest.MyStaticReceiver";
    public static final String PERMISSION = "com.lpc.broadcastreceivertest.MyStaticReceiver";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        init();
    }

    private void init() {

    }

    public void click3(View view) {

        if (PermissionChecker.checkSelfPermission(this, PERMISSION) == PermissionChecker.PERMISSION_GRANTED) {
            Intent i = new Intent();
            i.setAction(STATIC_ACTION);
            sendBroadcast(i);
        } else {
            Log.e("lipc0113", "没有权限");
            ActivityCompat.requestPermissions(this, new String[]{PERMISSION}, 100);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 100 && grantResults[0] == PermissionChecker.PERMISSION_GRANTED) {

            click3(null);
        }
    }

}
